package at.ac.tuwien.big.forms.htmlgen;

import at.ac.tuwien.big.forms.Attribute;
import at.ac.tuwien.big.forms.AttributePageElement;
import at.ac.tuwien.big.forms.AttributeType;
import at.ac.tuwien.big.forms.Column;
import at.ac.tuwien.big.forms.DateSelectionField;
import at.ac.tuwien.big.forms.Enumeration;
import at.ac.tuwien.big.forms.FormModel;
import at.ac.tuwien.big.forms.Literal;
import at.ac.tuwien.big.forms.Page;
import at.ac.tuwien.big.forms.PageElement;
import at.ac.tuwien.big.forms.RelationshipPageElement;
import at.ac.tuwien.big.forms.SelectionField;
import at.ac.tuwien.big.forms.TextArea;
import at.ac.tuwien.big.forms.TextField;
import at.ac.tuwien.big.forms.TimeSelectionField;
import com.google.common.base.Objects;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;

@SuppressWarnings("all")
public class Form2HTMLGenerator implements IGenerator {
  public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
    ResourceSet _resourceSet = resource.getResourceSet();
    final EList<Resource> listOfModels = _resourceSet.getResources();
    FormModel formModel = ((FormModel) null);
    for (final Resource model : listOfModels) {
      EList<EObject> _contents = model.getContents();
      EObject _get = _contents.get(0);
      if ((_get instanceof FormModel)) {
        EList<EObject> _contents_1 = model.getContents();
        EObject _get_1 = _contents_1.get(0);
        formModel = ((FormModel) _get_1);
      }
    }
    final File name = new File("output.html");
    String _string = name.toString();
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<!DOCTYPE html>");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("<html lang=\"en\">");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("�generateHead(formModel)�");
    _builder.newLine();
    _builder.append("          ");
    _builder.append("<body>");
    _builder.newLine();
    _builder.append("          ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("�FOR Form f : formModel.forms�");
    _builder.newLine();
    _builder.append("      ");
    _builder.append("<div class=\"form\" id=\"�f.name�\">");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("<form action=\"#\" class=\"register\">");
    _builder.newLine();
    _builder.append("          ");
    _builder.append("<h1>�f.title�</h1>");
    _builder.newLine();
    _builder.append("          ");
    _builder.append("<h2>�f.description�</h2>");
    _builder.newLine();
    _builder.append("          ");
    _builder.append("�generatePages(f.pages)�");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("</form>");
    _builder.newLine();
    _builder.append("      ");
    _builder.append("</div>");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("�ENDFOR�");
    _builder.newLine();
    _builder.append("     ");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("</body>");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("</html>");
    fsa.generateFile(_string, _builder);
  }
  
  public CharSequence generatePages(final List<Page> pages) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("�FOR Page p : pages�");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("<div class=\"page\" id=\"�p.title�\">");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("<fieldset class=\"row1\">");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("<h3>�p.title�</h3>");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("�generatePageElements(p.pageElements)�");
    _builder.newLine();
    _builder.append("    ");
    _builder.append("</fieldset>");
    _builder.newLine();
    _builder.append("  ");
    _builder.append("</div>");
    _builder.newLine();
    _builder.append("�ENDFOR�");
    _builder.newLine();
    return _builder;
  }
  
  public void generatePageElements(final EList<PageElement> list) {
    for (final PageElement pe : list) {
      if ((pe instanceof AttributePageElement)) {
        this.generatePageElement(pe);
      } else {
        if ((pe instanceof RelationshipPageElement)) {
          this.generatePageElement(pe);
        }
      }
    }
  }
  
  protected CharSequence _generatePageElement(final AttributePageElement element) {
    CharSequence _xifexpression = null;
    if ((element instanceof TextField)) {
      _xifexpression = this.generateAttributePageElement(((TextField)element));
    } else {
      CharSequence _xifexpression_1 = null;
      if ((element instanceof DateSelectionField)) {
        _xifexpression_1 = this.generateAttributePageElement(((DateSelectionField)element));
      } else {
        CharSequence _xifexpression_2 = null;
        if ((element instanceof TimeSelectionField)) {
          _xifexpression_2 = this.generateAttributePageElement(((TimeSelectionField)element));
        } else {
          CharSequence _xifexpression_3 = null;
          if ((element instanceof TextArea)) {
            _xifexpression_3 = this.generateAttributePageElement(((TextArea)element));
          } else {
            CharSequence _xifexpression_4 = null;
            if ((element instanceof SelectionField)) {
              _xifexpression_4 = this.generateAttributePageElement(((SelectionField)element));
            }
            _xifexpression_3 = _xifexpression_4;
          }
          _xifexpression_2 = _xifexpression_3;
        }
        _xifexpression_1 = _xifexpression_2;
      }
      _xifexpression = _xifexpression_1;
    }
    return _xifexpression;
  }
  
  public CharSequence generateAttributePageElement(final TextField element) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<p>");
    _builder.newLine();
    _builder.append("<label for=\"");
    String _elementID = element.getElementID();
    _builder.append(_elementID, "");
    _builder.append("\">");
    String _label = element.getLabel();
    _builder.append(_label, "");
    _builder.append(" ");
    CharSequence _xifexpression = null;
    Attribute _attribute = element.getAttribute();
    boolean _isMandatory = _attribute.isMandatory();
    if (_isMandatory) {
      _xifexpression = this.generateMandatoryStar();
    }
    _builder.append(_xifexpression, "");
    _builder.append("</label>");
    _builder.newLineIfNotEmpty();
    _builder.append("<input type=\"text\" id=\"");
    String _elementID_1 = element.getElementID();
    _builder.append(_elementID_1, "");
    _builder.append("\" ");
    CharSequence _xifexpression_1 = null;
    Attribute _attribute_1 = element.getAttribute();
    boolean _isMandatory_1 = _attribute_1.isMandatory();
    if (_isMandatory_1) {
      _xifexpression_1 = this.generateMandatoryClass();
    }
    _builder.append(_xifexpression_1, "");
    _builder.append("/>");
    _builder.newLineIfNotEmpty();
    _builder.append("</p>");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence generateMandatoryClass() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("class=\"mandatory\"");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence generateMandatoryStar() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<span>*</span>");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence generateAttributePageElement(final DateSelectionField element) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<p>");
    _builder.newLine();
    _builder.append("<label for=\"");
    String _elementID = element.getElementID();
    _builder.append(_elementID, "");
    _builder.append("\">");
    Attribute _attribute = element.getAttribute();
    String _name = _attribute.getName();
    _builder.append(_name, "");
    _builder.append("</label>");
    _builder.newLineIfNotEmpty();
    _builder.append("<input type=\"date\" id=\"");
    String _elementID_1 = element.getElementID();
    _builder.append(_elementID_1, "");
    _builder.append("\"/>");
    _builder.newLineIfNotEmpty();
    _builder.append("</p>");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence generateAttributePageElement(final TimeSelectionField element) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<p>");
    _builder.newLine();
    _builder.append("<label for=\"");
    String _elementID = element.getElementID();
    _builder.append(_elementID, "");
    _builder.append("\">");
    Attribute _attribute = element.getAttribute();
    String _name = _attribute.getName();
    _builder.append(_name, "");
    _builder.append("</label>");
    _builder.newLineIfNotEmpty();
    _builder.append("<input type=\"time\" id=\"");
    String _elementID_1 = element.getElementID();
    _builder.append(_elementID_1, "");
    _builder.append("\"/>");
    _builder.newLineIfNotEmpty();
    _builder.append("</p>");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence generateAttributePageElement(final TextArea element) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<p>");
    _builder.newLine();
    _builder.append("<label for=\"");
    String _elementID = element.getElementID();
    _builder.append(_elementID, "");
    _builder.append("\">");
    String _label = element.getLabel();
    _builder.append(_label, "");
    _builder.append(" ");
    CharSequence _xifexpression = null;
    Attribute _attribute = element.getAttribute();
    boolean _isMandatory = _attribute.isMandatory();
    if (_isMandatory) {
      _xifexpression = this.generateMandatoryStar();
    }
    _builder.append(_xifexpression, "");
    _builder.append("</label>");
    _builder.newLineIfNotEmpty();
    _builder.append("<textarea id=\"");
    String _elementID_1 = element.getElementID();
    _builder.append(_elementID_1, "");
    _builder.append("\" ");
    CharSequence _xifexpression_1 = null;
    Attribute _attribute_1 = element.getAttribute();
    boolean _isMandatory_1 = _attribute_1.isMandatory();
    if (_isMandatory_1) {
      _xifexpression_1 = this.generateMandatoryClass();
    }
    _builder.append(_xifexpression_1, "");
    _builder.append("/>");
    _builder.newLineIfNotEmpty();
    _builder.append("</p>");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence generateAttributePageElement(final SelectionField element) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<p>");
    _builder.newLine();
    _builder.append("<label for=\"");
    String _elementID = element.getElementID();
    _builder.append(_elementID, "");
    _builder.append("\">");
    String _label = element.getLabel();
    _builder.append(_label, "");
    _builder.append(" ");
    CharSequence _xifexpression = null;
    Attribute _attribute = element.getAttribute();
    boolean _isMandatory = _attribute.isMandatory();
    if (_isMandatory) {
      _xifexpression = this.generateMandatoryStar();
    }
    _builder.append(_xifexpression, "");
    _builder.append("</label>");
    _builder.newLineIfNotEmpty();
    _builder.append("<select id=\"");
    String _elementID_1 = element.getElementID();
    _builder.append(_elementID_1, "");
    _builder.append("\" name=\"");
    Attribute _attribute_1 = element.getAttribute();
    String _name = _attribute_1.getName();
    _builder.append(_name, "");
    _builder.append("\" ");
    CharSequence _xifexpression_1 = null;
    Attribute _attribute_2 = element.getAttribute();
    boolean _isMandatory_1 = _attribute_2.isMandatory();
    if (_isMandatory_1) {
      _xifexpression_1 = this.generateMandatoryClass();
    }
    _builder.append(_xifexpression_1, "");
    _builder.append("/>");
    _builder.newLineIfNotEmpty();
    _builder.append("<option value=\"default\"> </option>");
    _builder.newLine();
    CharSequence _xifexpression_2 = null;
    Attribute _attribute_3 = element.getAttribute();
    AttributeType _type = _attribute_3.getType();
    boolean _equals = Objects.equal(_type, AttributeType.NONE);
    if (_equals) {
      Attribute _attribute_4 = element.getAttribute();
      Enumeration _enumeration = _attribute_4.getEnumeration();
      EList<Literal> _literals = _enumeration.getLiterals();
      for (final Literal l : _literals) {
        this.generateLiteral(l);
      }
    } else {
      CharSequence _xifexpression_3 = null;
      Attribute _attribute_5 = element.getAttribute();
      AttributeType _type_1 = _attribute_5.getType();
      boolean _equals_1 = Objects.equal(_type_1, AttributeType.BOOLEAN);
      if (_equals_1) {
        _xifexpression_3 = this.generateLiteralBoolean();
      }
      _xifexpression_2 = _xifexpression_3;
    }
    _builder.append(_xifexpression_2, "");
    _builder.newLineIfNotEmpty();
    _builder.append("</p>");
    _builder.newLine();
    return _builder;
  }
  
  public CharSequence generateLiteralBoolean() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<option value=\"");
    _builder.append(true, "");
    _builder.append("\">yes</option>");
    _builder.newLineIfNotEmpty();
    _builder.append("<option value=\"");
    _builder.append(false, "");
    _builder.append("\">no</option>");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  public CharSequence generateLiteral(final Literal literal) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<option value=\"");
    String _name = literal.getName();
    _builder.append(_name, "");
    _builder.append("\">");
    String _value = literal.getValue();
    _builder.append(_value, "");
    _builder.append("</option>");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  public CharSequence generateAttributePageElement(final Column column) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<th>");
    Attribute _attribute = column.getAttribute();
    String _name = _attribute.getName();
    _builder.append(_name, "");
    _builder.append("</th>");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  protected CharSequence _generatePageElement(final RelationshipPageElement element) {
    throw new UnsupportedOperationException("TODO: auto-generated method stub");
  }
  
  public CharSequence generateHead(final FormModel formModel) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<head>");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("<title>Form</title>");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"/>");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"/>");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"../assets/form.css\"/>");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("<script src=\"../assets/jquery-1.10.2.min.js\" type=\"text/javascript\"></script>");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("<script src=\"../assets/form.js\" type=\"text/javascript\"></script>");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("<script type=\"text/javascript\">");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("$(document).ready(");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("function(){         ");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("�FOR f : formModel.forms�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t");
    _builder.append("�IF f.welcomeForm�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t");
    _builder.append("form.addWelcomeForm(\'�f.title�\');");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t");
    _builder.append("�ENDIF�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("�ENDFOR�");
    _builder.newLine();
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("�FOR f : formModel.forms�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t");
    _builder.append("�FOR p : f.pages�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t");
    _builder.append("�FOR pe : p.pageElements�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t\t\t");
    _builder.append("�IF pe instanceof at.ac.tuwien.big.forms.TextField�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t\t\t");
    _builder.append("form.addRegularExpression(\'�pe.elementID�\', \'�pe.format�\');");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t\t");
    _builder.append("�ENDIF�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t");
    _builder.append("�ENDFOR�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t");
    _builder.append("�ENDFOR�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("�ENDFOR�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("�FOR f : formModel.forms�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t");
    _builder.append("�FOR p : f.pages�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t");
    _builder.append("�FOR pe : p.pageElements�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t\t");
    _builder.append("�IF pe instanceof Table�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t\t\t");
    _builder.append("form.addRelationshipPageElement (\'�p.title�>\', \'�pe.elementID�\', \'�pe.editingForm.title�\', \'table\', \'�pe.relationship.lowerBound�\', \'�pe.relationship.upperBound�\');");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t\t");
    _builder.append("�ENDIF�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t\t");
    _builder.append("�ENDFOR�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t\t");
    _builder.append("�ENDFOR�");
    _builder.newLine();
    _builder.append("\t\t\t\t\t");
    _builder.append("�ENDFOR�");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("form.init();");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("});");
    _builder.newLine();
    _builder.append("        ");
    _builder.append("</script>");
    _builder.newLine();
    _builder.append("      ");
    _builder.append("</head>");
    return _builder;
  }
  
  public CharSequence generatePageElement(final PageElement element) {
    if (element instanceof AttributePageElement) {
      return _generatePageElement((AttributePageElement)element);
    } else if (element instanceof RelationshipPageElement) {
      return _generatePageElement((RelationshipPageElement)element);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(element).toString());
    }
  }
}
